const puppeteer = require('puppeteer');

module.exports =async function getPuppeteerImageData(url, selector) {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);

    let lastHeight = await page.evaluate('document.body.scrollHeight');
    while (true) {
        await page.evaluate('window.scrollTo(0, document.body.scrollHeight)');
        await page.waitForTimeout(500);
        const newHeight = await page.evaluate('document.body.scrollHeight');
        if (newHeight === lastHeight) {
            break;
        }
        lastHeight = newHeight;
    }

    const data = await page.evaluate((selector) => {
        const elements = Array.from(document.querySelectorAll(selector));
        return elements.map(element => {
            const imageUrl = element.getAttribute('src');
            const extension = imageUrl.split('.').pop();
            return {
                url: imageUrl,
                extension,
            };
        });
    }, selector);

    await browser.close();
    return data;
}


